package com.my.myTradeData.DataSource;

import java.math.BigDecimal;

import org.apache.commons.math3.distribution.BinomialDistribution;

public class Option {
	private double price;
	private double step;

	private double delta;

	private double cPrice;
	private double pPrice;

	public double getcPrice() {
		return cPrice;
	}

	public void setcPrice(double cPrice) {
		this.cPrice = cPrice;
	}

	public double getpPrice() {
		return pPrice;
	}

	public void setpPrice(double pPrice) {
		this.pPrice = pPrice;
	}

	public double getDelta() {
		return delta;
	}

	public void setDelta(double delta) {
		this.delta = delta;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getStep() {
		return step;
	}

	public void setStep(double step) {
		this.step = step;
	}

	private double beginPrice;

	public double getBeginPrice() {
		return beginPrice;
	}

	public void setBeginPrice(double beginPrice) {
		this.beginPrice = beginPrice;
	}

	public static void main(String[] args) {

//		String data = CUrlUtil.get("http://hq.sinajs.cn/list=I2105",
//				"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36");
//
//		double beginPrice = Double.parseDouble(data.split(",")[8]);
//
//		String code = "i2105";
//
//		data = CUrlUtil.get(
//				"http://stock.finance.sina.com.cn/futures/api/openapi.php/OptionService.getOptionData?type=futures&product=i_o&exchange=dce&pinzhong="
//						+ code,
//				"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36");
//
//		for (String d : data.split("\\],\\[")) {
//
//			System.out.println(d);
//		}
		// code,beginPrice,optionPrice,cPrice,pPrice,step,day
		int dayoffset = 0;
		String datas[] = { "50etf,2.669,2.6,0.0045,0.0042,6,0.0877" };
		double atr[] = { 0.0039, 0.003, 0.0056, 0.0143, 0.0168, 0.0250, 0.0369 };
		int[] minuteSteps = { 3, 5, 15, 30, 60, 120, 240 };
		for (String data : datas) {
			if (!data.split(",")[0].equals("50etf")) {
				continue;
			}

			double beginPrice = Double.parseDouble(data.split(",")[1]);
			double optionPrice = Double.parseDouble(data.split(",")[2]);
			double cPrice = Double.parseDouble(data.split(",")[3]);
			double pPrice = Double.parseDouble(data.split(",")[4]);
			int day = Integer.parseInt(data.split(",")[5]);
			double delta = Double.parseDouble(data.split(",")[6]);

			String code = data.split(",")[0];
			Option s = new Option();
			s.setPrice(beginPrice);
			s.setcPrice(cPrice);
			s.setpPrice(pPrice);

			int times = 0;
			if (code.contains("etf")) {
				times = 240;
			} else if (code.contains("铝") || code.contains("铜") || code.contains("锌")) {
				times = 465;
			} else if (code.contains("橡胶") || code.contains("玉米") || code.contains("铁矿石") || code.contains("塑料")
					|| code.contains("豆粕") || code.contains("液化气") || code.contains("pp") || code.contains("pvc")
					|| code.contains("棉花") || code.contains("甲醇") || code.contains("菜粕") || code.contains("白糖")
					|| code.contains("pta") || code.contains("动力煤")) {
				times = 345;
			} else if (code.contains("黄金")) {
				times = 555;
			}
			System.out.println("概率:" + delta);
			for (int i = 0; i < 7; i++) {
				int t = times * (day - dayoffset) / minuteSteps[i];
				BinomialDistribution db1 = new BinomialDistribution(t, 0.5);
				System.out.println("总次数:" + (t));
				System.out.println("成功次数:" + db1.inverseCumulativeProbability(delta));

				int bt = t - db1.inverseCumulativeProbability(delta) * 2;
				System.out.println("波动次数:" + (bt));

				double cP = beginPrice - (atr[i] * bt);
//				double currentP = optionPrice + s.getcPrice();
				double currentP = optionPrice - s.getpPrice();
				System.out.println(code + " " + (currentP - cP));
			}

		}

	}
}
